package hauwei.year2025.month4;

import java.util.*;

public class UpdateTimePro {
    public static HashMap<String,String> codeMap;
    public static int max=Integer.MIN_VALUE;
    public static TreeSet<String> res=new TreeSet<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        codeMap = new HashMap<>(n);
        for (int i = 0; i < n; i++) {
            String s = sc.nextLine();
            String[] split = s.split(" ");
            codeMap.put(split[0],split[1]);
        }
        sc.close();
        Set<String> starts = codeMap.keySet();
        for(String start:starts){
            String next=codeMap.get(start);
            int sum=0;
            while(next!=null&&!next.equals("NA")){
                sum++;
                next=codeMap.get(next);
            }
            if(sum>max){
                res.clear();
                res.add(start);
                max=sum;
            } else if (sum == max) {
                res.add(start);
            }
        }
        Iterator<String> iterator = res.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next());
            if(iterator.hasNext()){
                System.out.print(" ");
            }
        }
    }
}
